<?php

namespace App\Http\Controllers\Company;

use App\Models\Category;
use App\Models\CompanyProfile;
use App\Http\Controllers\Core\HomeController as Home;
use App\Models\Job;
use App\Models\JobResumeAccess;
use App\Models\Resume;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class HomeController extends Home
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        if (!$this->isLogin()) {
            return redirect('login');
        }
        $company = CompanyProfile::where(['user_id' => $this->member->id])->get();
        if (!$company = $company->first()) {
            return redirect('login');
        };

        //招聘中的职位
        $jobNumber = Job::where(['display' => 1, 'user_id' => $this->member->id])->count();
        $jobStopNumber = Job::where(['user_id' => $this->member->id])->count();
        $resumeNumber = JobResumeAccess::where(['company_id' => $company->id, 'user_id' => $this->member->id, 'status' => 1])->count();
        $resumeStopNumber = JobResumeAccess::where(['company_id' => $company->id, 'user_id' => $this->member->id])->count();
        //系统推荐职位
        $groom = Resume::where('trade', $company->trade)->get();
//        dd($groom);
        //获取所有分类下的
        $tags = Category::getTag($company->tag);
        $data = [
            'jobNumber' => $jobNumber,
            'jobStopNumber' => $jobStopNumber,
            'resumeNumber' => $resumeNumber,
            'resumeStopNumber' => $resumeStopNumber,
            'member' => $this->member,
            'company' => $company,
            'tags' => $tags,
            'groom' => $groom
        ];
        return view('company.home.index', $data);
    }

    /**
     * 推荐返回
     */
    public function groom()
    {
        if (!$this->isLogin()) {
            return redirect('login');
        }
        $company = CompanyProfile::where(['user_id' => $this->member->id])->get();
        if (!$company = $company->first()) {
            return redirect('login');
        };
        $groom = Resume::where('trade', $company->trade)->orderByRaw("RAND()")->get();
        $data['html'] = response(view('company.ajax.groom', ['groom' => $groom]))->getContent();
        return $this->retJson(1, '成功', $data);
    }

    /**
     * 验证是否是付费用户
     * @param Request $request
     */
    public function pay(Request $request)
    {

        $data = [
            'title1' => "您的信息不完整",
            'title2' => "请填写完整信息，或者等待招聘顾问与您联系",
            'title3' => "签约后可发布新职位",
            'mobile' => "0571-65817457"
        ];
        // 1已经填写 内容 2 未添加内容 3 已经审批
        $user_id = $this->getUserId();
        //获取用户状态
        $company = CompanyProfile::where(['user_id' => $user_id])->get()->first();
        if (isSet($company->audit) && $company->audit == 1) {
            echo $this->retJson(1, '成功');
            exit;
        }
        if (!isSet($company->city)) {
            $data['html'] = response(view('company.home.pay', $data))->getContent();
            echo $this->retJson(2, '失败', $data);
            exit;
        }
        $data['title1'] = '您的企业信息不完整';
        $data['title2'] = '请等候招聘顾问主动与您联系';
        $data['title3'] = '签约后可发布新职位';
        $data['html'] = response(view('company.home.pay', $data))->getContent();
        echo $this->retJson(0, '失败', $data);
        exit;


    }

    /**
     * refresh 刷新简历
     */
    public function refresh()
    {
        if (!$this->isLogin()) {
            return redirect('login');
        }
        $user_id = $this->getUserId();
        //通过user_id获取company_id
        $company = CompanyProfile::where(['user_id' => $user_id])->get();
        Job::where('views', $user_id)->update(['views' => 100]);
    }

}